package com.qk.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.BusinessService;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessService businessService;

    /**
     * 查询线索列表
     */
    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
//        PageHelper.startPage(clueDto.getPage(),clueDto.getPageSize());
//        Page<Clue> page =(Page<Clue>) clueMapper.listClues(clueDto);
//        return new PageResult<>(page.getTotal(),page.getResult());

        Page<Clue> page = clueMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);

        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    /**
     * 根据id查询线索
     */
    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }

    /**
     * 线索跟进
     */
    //@Transactional 事务控制，使用场景：在一次操作中多次对数据库进行增删改操作
    // @Transactional 默认只对运行时异常和错误进行回滚，如果希望对其它异常进行回滚，需要设置rollbackFor属性，示例如下
    // @Transactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackClue(Clue clue) {
        //1.根据id更新线索的基本信息
        clue.setStatus(3);//跟进中
        clue.setUpdateTime(LocalDateTime.now());
        //clueMapper.updateById(clue);
        this.updateById(clue);

        //2.添加线索跟进记录   插入一条数据到线索跟进记录表
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());//线索id
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        //clue是从前端获取的，clue的内容都来自填写的数据，而填写的内容里面并没有clue.userId，需要从别的地方获得，

        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);//1-正常跟进
        clueTrackRecord.setCreateTime(LocalDateTime.now());

        clueTrackRecordMapper.insert(clueTrackRecord);

    }

    /**
     * 伪线索处理
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void falseClue(Integer id, MarkFalseClueDto markFalseClueDto) {
        //1.根据id更新线索的状态为4
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        //clueMapper.updateById(clue);
        this.updateById(clue);

        //2.添加线索跟进记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);//线索id
        clueTrackRecord.setFalseReason(markFalseClueDto.getReason());//伪线索原因
        clueTrackRecord.setRecord(markFalseClueDto.getRemark());//备注说明
        clueTrackRecord.setType(0);//0-伪线索
        clueTrackRecord.setCreateTime(LocalDateTime.now());//添加时间
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//获取当前用户id.跟进人id

        clueTrackRecordMapper.insert(clueTrackRecord);

    }

    /**
     * 线索转商机
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toBusiness(Integer id) {
/* 我写的
        //1.根据id更新线索的状态为5
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        //clueMapper.updateById(clue);
        this.updateById(clue);

        //2.添加线索跟进记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);

        //3.获取更新后的线索信息
        Clue clue1 = this.getClueById(id);

        //4.添加商机信息
        Business business = new Business();
        business.setName(clue1.getName());
        business.setPhone(clue1.getPhone());
        business.setGender(clue1.getGender());
        business.setAge(clue1.getAge());
        business.setWechat(clue1.getWechat());
        business.setQq(clue1.getQq());
        business.setSubject(clue1.getSubject());
        business.setChannel(clue1.getChannel());
        business.setClueId(clue1.getId());
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        //5.添加商机到商机表
        businessService.save(business);*/

        //优化
        //1.根据id更新线索的状态为5
        Clue clue = clueMapper.getClueById(id);

        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        //clueMapper.updateById(clue);
        this.updateById(clue);

        //4.添加商机信息
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);//属性拷贝，Spring提供的工具类

        business.setId( null);
        business.setUserId(null);
        business.setClueId(id);
        business.setStatus(1);
        business.setNextTime( null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        //5.添加商机到商机表
        businessService.save(business);
    }

    /**
     * 线索池列表
     */
    @Override
    public PageResult<Clue> poolList(ClueDto clueDto) {
        Page<Clue> page = clueMapper.poolList(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(),page.getRecords()) ;
    }


}
